package com.wtwd.campus.attendancesend;

import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.AttendanceCommand;
import com.wtwd.campus.entity.MqMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeAdjustingSender implements AttendanceSender {
    @Override
    public AttendanceCommand dealMessage(MqMessage mqMessage) {
        String productNo = (String) mqMessage.getObject();
        String length="0007";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        Date time=new Date(System.currentTimeMillis());
        String format = sdf.format(time);
        String date=dateConvertToHexStr(format);
        String content="5A".concat(Constants.TIME_CHECK).concat(length).concat(date);
        String xor=getXOR(content.substring(2));
        content=content.concat(xor.substring(2,4)).concat("CA");
        AttendanceCommand command=new AttendanceCommand();
        command.setProductNo(productNo);
        command.setContent(content);
        return command;
    }

    /**
     * 将年月日时分秒转成十六进制字符串
     * @param date
     * @return
     */
    public String dateConvertToHexStr(String date){
        String year = date.substring(0, 4);
        String y = String.format("%04X", Integer.valueOf(year));
        String month = date.substring(4, 6);
        String m = String.format("%02X", Integer.valueOf(month));
        String day = date.substring(6, 8);
        String d = String.format("%02X", Integer.valueOf(day));
        String hour = date.substring(8, 10);
        String h = String.format("%02X", Integer.valueOf(hour));
        String min = date.substring(10, 12);
        String mi = String.format("%02X",Integer.valueOf( min));
        String second = date.substring(12, 14);
        String s = String.format("%02X", Integer.valueOf(second));
        return y.concat(m).concat(d).concat(h).concat(mi).concat(s);
    }

}
